package com.software.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.software.base.ResultInfo;
import com.software.bean.ProductBean;
import com.software.dto.ProductReleaseDto;
import com.software.dto.UpdateProductDto;
import com.software.dto.UpdateProductHomeDisplayDto;
import com.software.service.ProductService;
import com.software.utils.AssertUtil;
import com.software.vo.ProductDetailVo;
import com.software.vo.ProductVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wyj
 * @since 2024-03-24
 */

@Controller
@RequestMapping("/product")
public class ProductController {


    @Autowired
    private ProductService productService;

    /**
     * @description:发布商品
     * @author: wyj
     * @date: 2024/4/1 19:38
     * @param:
     * @return:返回pid,pid用于插入商品详情中的多张图片
     */
    @RequestMapping("/front/postProductRelease")
    @ResponseBody
    public ResultInfo postProductRelease(ProductReleaseDto releaseDto) {
        Integer pid = productService.postProductRelease(releaseDto);
        AssertUtil.isTrue(pid == null, "返回值异常");
        return ResultInfo.success(pid);
    }


    /**
     * @description:小程序我的发布列表
     * @author: wyj
     * @date: 2024/4/1 22:11
     * @param:
     * @return:
     */
    @GetMapping("/front/getReleaseListByCid")
    @ResponseBody
    public ResultInfo getReleaseListByCid(@RequestParam Integer customerId) {
        List<ProductBean> list = productService.getReleaseListByCid(customerId);
        return ResultInfo.success(list);
    }

    /**
     * @description:后台管理系统审核列表
     * @author: wyj
     * @date: 2024/4/1 22:09
     * @param:
     * @return:
     */
    @GetMapping("/api/getReleaseProductList")
    @ResponseBody
    public ResultInfo getReleaseProductList() {
        List<ProductBean> list = productService.getReleaseProductList();
        return ResultInfo.success(list);
    }

    /**
     * @description:拒绝审核通过设置status=5
     * @author: wyj
     * @date: 2024/4/2 0:28
     * @param:
     * @return:
     */
    @DeleteMapping("/api/auditRejected/{id}")
    @ResponseBody
    public ResultInfo auditRejected(@PathVariable Integer id) {
        Integer result = productService.auditRejected(id);
        AssertUtil.isTrue(result != 1, "审核失败");
        return ResultInfo.success();
    }

    /**
     * @description:批量审核通过
     * @author: wyj
     * @date: 2024/4/11 16:04
     * @param:
     * @return:
     */
    @PutMapping("/api/auditBatch")
    @ResponseBody
    public ResultInfo auditBatch(@RequestBody Integer auditIds[]) {
        Integer result = productService.auditBatch(auditIds);
        AssertUtil.isTrue(result != 1, "审核失败1");
        return ResultInfo.success();
    }

    /**
     * @description:展示列表
     * @author: wyj
     * @date: 2024/4/2 16:20
     * @param:
     * @return:
     */
    @GetMapping("/api/getAllProductDisplayList")
    @ResponseBody
    public ResultInfo getAllProductDisplayList(Integer currentPageNum, Integer currentPageSize) {
        IPage<ProductBean> iPage = productService.getAllProductDisplayList(currentPageNum, currentPageSize);
        return ResultInfo.success((int) iPage.getTotal(), iPage.getRecords());
    }

    /**
     * @description:后端主页显示功能
     * @author: wyj
     * @date: 2024/4/2 17:55
     * @param:productId,isHome
     * @return:
     */
    @PutMapping("/api/updateProductHomeDisplay")
    @ResponseBody
    public ResultInfo updateProductHomeDisplay(@RequestBody UpdateProductHomeDisplayDto updateProductHomeDisplayDto) {
        Integer result = productService.updateProductHomeDisplay(updateProductHomeDisplayDto.getProductId(), updateProductHomeDisplayDto.getIsHome());
        AssertUtil.isTrue(result != 1, "返回值异常");
        return ResultInfo.success();
    }

    /**
     * @description:主页所有展示中的商品实现下拉加载更多
     * @author: wyj
     * @date: 2024/4/2 20:34
     * @param:isHome=1
     * @return:
     */
    @GetMapping("/front/getAllDisplayProduct")
    @ResponseBody
    public ResultInfo getAllDisplayProduct(Integer pageSize, Integer pageNum) {

        IPage<ProductBean> page = productService.getAllDisplayProduct(pageSize, pageNum);

        return ResultInfo.success((int) page.getPages(), page.getRecords());
    }

    /**
     * @description:前端闲置物品详情
     * @author: wyj
     * @date: 2024/4/2 21:53
     * @param:productId
     * @return:
     */
    @GetMapping("/front/getProductDetailByProductId")
    @ResponseBody
    public ResultInfo getProductDetailByProductId(Integer productId) {
        ProductDetailVo productDetailVo = productService.getProductDetailByProductId(productId);
        return ResultInfo.success(productDetailVo);
    }

    /**
     * @description:根据分类查询商品
     * @author: wyj
     * @date: 2024/4/3 15:25
     * @param:
     * @return:
     */
    @RequestMapping("/front/getProductListByCategoryId/{categoryId}")
    @ResponseBody
    public ResultInfo getCategoryProductListByCategoryId(@PathVariable Integer categoryId) {
        System.out.println(categoryId);
        List<ProductBean> list = productService.getProductListByCategoryId(categoryId);
        return ResultInfo.success(list);
    }

    /**
     * @description:搜索功能实现
     * @author: wyj
     * @date: 2024/4/5 17:08
     * @param:
     * @return:
     */
    @GetMapping("/front/getSearchByContent")
    @ResponseBody
    public ResultInfo getSearchByContent(@RequestParam("content") String content) {
        List<ProductBean> list = productService.getSearchByContent(content);
        return ResultInfo.success(list);
    }

    /**
     * @description:近七天发布的数量统计—————饼图
     * @author: wyj
     * @date: 2024/4/6 15:33
     * @param:
     * @return:
     */

    @GetMapping("/api/getSevenDaysReleaseCountGroupByCategory")
    @ResponseBody
    public ResultInfo getSevenDaysReleaseCountGroupByCategory() {
        List<Map<String, Object>> list = productService.getSevenDaysReleaseCountGroupByCategory();
        return ResultInfo.success(list);
    }

    /**
     * @description:撤销审核
     * @author: wyj
     * @date: 2024/4/11 16:05
     * @param:
     * @return:
     */
    @RequestMapping("/api/removeAudit/{productId}")
    @ResponseBody
    public ResultInfo removeAudit(@PathVariable Integer productId) {
        Integer result = productService.removeAudit(productId);
        AssertUtil.isTrue(result != 1, "返回值异常");
        return ResultInfo.success();
    }

    /**
     * @description:删除商品信息
     * @author: wyj
     * @date: 2024/4/13 14:25
     * @param:
     * @return:
     */
    @RequestMapping("/front/deleteProductById/{productId}")
    @ResponseBody
    public ResultInfo deleteProductById(@PathVariable Integer productId) {
        Integer result = productService.deleteProductById(productId);
        AssertUtil.isTrue(result != 1, "删除失败");
        return ResultInfo.success();
    }

    /**
     * @description:获取商品信息去编辑
     * @author: wyj
     * @date: 2024/4/13 14:25
     * @param:
     * @return:
     */
    @GetMapping("/front/getProductInfoByIdForEdit/{pid}")
    @ResponseBody
    public ResultInfo getProductInfoByIdForEdit(@PathVariable Integer pid) {
        ProductVo productVo = productService.getProductInfoByIdForEdit(pid);
        AssertUtil.isTrue(productVo == null, "获取商品信息失败");
        return ResultInfo.success(productVo);
    }

    /** @description:提交更新
      * @author: wyj
      * @date: 2024/4/16 18:05
      * @param:
      * @return:
      */
    @PostMapping("/front/updateProduct")
    @ResponseBody
    public ResultInfo updateProduct( UpdateProductDto updateProductDto) {
        Integer pid = productService.updateProduct(updateProductDto);
        return ResultInfo.success(pid);
    }
}

